<?php

/**
 * @author FROSTY (valik619)
 * @site nadmad.ru
 * @mail valik619@inbox.ru
 * @Modul: Mail v1.0
 */
$title = 'WBCMS - wap blogs CMS';
$root = '../';
include('../system/core.php');

$action=htmlspecialchars(trim($_GET['a']));

switch ($action){

default:

include('../system/head.php');

$pr=$_GET['id'];  
$nameuser = mysql_query("SELECT * FROM `users` WHERE `id` = '$pr'");
$nc = mysql_fetch_assoc($nameuser);
if(!mysql_num_rows($nameuser)){
    echo 'ERROR! User not register!'; break;
}

echo '<div class="title">'.$lang['dialog_with'].''.$nc['nick'].'</div>';

echo '<div id="imenu">';
$ignorlist=mysql_result(mysql_query("SELECT COUNT(*) FROM `ignor` WHERE `uid` = '$pr' AND `pid` = '$uid'"), 0);
if(!$ignorlist){
echo '<form action="index.php?a=say&amp;id='.$pr.'" method="post">';
include($root.'system/inc/auto.php'); //Подключаем автовставку бб кодов
				echo '</p>';
echo '<textarea name="soft" id="post"></textarea><br />';

echo '<p><input type="submit" name="submit" value="'.$lang['add'].'"/></form></p>';
}
$posts = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail`  WHERE (`adresat`='$uid' AND `author`='$pr' AND `type` = 'in') OR (`author`='$uid' AND `adresat`='$pr' AND `type` = 'out')"), 0);
//$posts1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE `adresat` = '$uid' AND `author` = '$pr'"), 0);
//echo "Відправлені: $posts Вхідні: $posts1";
if($posts > 0){
    
        //Моя навигация
    $page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ; 
    $allpage = ceil($posts/$kmess);
    $start = $page*$kmess - $kmess; 
$post = mysql_query("SELECT * FROM `mail` WHERE (`adresat`='$uid' AND `author`='$pr' AND `type` = 'in') OR (`author`='$uid' AND `adresat`='$pr' AND `type` = 'out') ORDER BY `time` DESC LIMIT $start, $kmess");
    
//$post=mysql_query("SELECT * FROM `mail` WHERE `adresat` = '$uid' AND `author` = '$pr' AND `author` = '$uid' AND `adresat` = '$pr' ORDER BY `time` DESC LIMIT $start,$kmess");
while($row=mysql_fetch_assoc($post)){
    echo '<p>';
    echo '<div id="comments">';
    
    if($row['author'] != 0){
    $usename = mysql_query("SELECT * FROM `users` WHERE `id` = '$row[author]'");
    $n = mysql_fetch_assoc($usename);
    echo '<div class="info"><a href="../office.php?a=anketa&amp;id='.$n['id'].'">'.$n['nick'].'</a>';
    
    if($n['online'] > (time() - 300)){
        echo '<span class="green">&nbsp;[ON]</span>';
    }else{
        echo '<span class="red">&nbsp;[OFF]</span>';
    }
    
    }else{
        echo '<div class="info"><a href="#">Guest</a>';
    }
        if($rights == 1 && $row['author'] == $uid){
        echo '<a href="index.php?a=delete&amp;id='.$row['id'].'" style="float: right;">X</a></div>';
    }else echo '</div>';
    
    $text = functions::bbcode($row['text']);
    echo '<div class="comm">'.functions::smileys($text).'</div>';
    if($row['chit'] == 0){
    echo '<span class="red"><small>'.date("d.m.Y / H:i", $row['time']).'</span>';
    }else{
    echo '<span class="green"><small>'.date("d.m.Y / H:i", $row['time']).'</span>';    
    }
    echo '</small>';
    
    echo '</div>'; //comments
    echo '</p>';

    
    };
}else{
    echo '<div class="info">'.$lang['mail_not'].'</div>';
}
echo '</div>'; //IMENU

if($posts>$kmess){
    echo '<p>';
    functions::navigation($allpage,"index.php?id=$pr&amp;p={p}",1);  
echo '</p>';}

echo '<div class="title"><a href="../office.php?a=anketa&amp;id='.$nc['id'].'">'.$nc['nick'].'</a></div>';

//Рахуємо прочитаними
mysql_query("UPDATE `mail` SET `chit` = '1' WHERE `adresat`='$uid' AND `author`='$pr'"); 

break;

case 'delignor':

$id=$_GET['id'];
$ignorlist=mysql_result(mysql_query("SELECT COUNT(*) FROM `ignor` WHERE `pid` = '$id' AND `uid` = '$uid'"), 0);
if(!$ignorlist){
    include('../system/head.php');
    echo '<div class="small"><a href="../office.php?a=anketa&amp;id='.$id.'" class="url">'.$lang['exists_ignor'].'</a></div>';
    break;
}
mysql_query("DELETE FROM `ignor` WHERE `uid` = '$uid' AND `pid` = '$id'");
header('Location: http://'.$set['home'].'/office.php?a=anketa&id='.$id.'');
break;

case 'ignor':

$id=$_GET['id'];
$ignorlist=mysql_result(mysql_query("SELECT COUNT(*) FROM `ignor` WHERE `pid` = '$pr' AND `uid` = '$uid'"), 0);
if($ignorlist){
    include('../system/head.php');
    echo '<div class="small"><a href="../office.php?a=anketa&amp;id='.$id.'" class="url">'.$lang['already_ignor'].'</a></div>';
    break;
}
mysql_query("INSERT INTO `ignor` (`id`, `uid`, `pid`) VALUES (NULL , '$uid', '$id');");
header('Location: http://'.$set['home'].'/office.php?a=anketa&id='.$id.'');
break;


case 'new':

$total = mysql_result(mysql_query("SELECT COUNT(*) FROM (SELECT DISTINCT `author` FROM `mail` WHERE `adresat`='$uid' AND `chit` = '0') a;"), 0);
if($total == 1) {
	//Если все новые сообщения от одного итого же чела показываем сразу переписку
	$max = mysql_result(mysql_query("SELECT `author`, count(*) FROM `mail` WHERE `adresat`='$uid'  AND `chit` = '0' GROUP BY `author`;"), 0);
	Header('Location: index.php?id='.$max);
	exit();
}


$zapros1 = mysql_query("SELECT * FROM `mail` WHERE (`chit` = '0' AND `adresat` = '$uid' AND `type` = 'in') GROUP BY `author`");

if(mysql_num_rows($zapros1) > 1){
    
    include('../system/head.php');
    echo '<div class="title">'.$lang['to_dialog'].'</div>';
while($z=mysql_fetch_assoc($zapros1)){
    $author = mysql_query("SELECT * FROM `users` WHERE `id` = '$z[author]'");
    $us = mysql_fetch_assoc($author);
    $skoko = mysql_result(mysql_query("SELECT COUNT(*) FROM `mail` WHERE `author` = '$z[author]' AND `type` = 'out'"), 0);

    echo '<div class="small"><a class="url" href="index.php?id='.$z['author'].'">'.$us['nick'].' +'.$skoko.'</a></div>';
    };
    
}else{
    header('Location: index.php?id='.$z['author'].'');
}
break;


case 'say':
$pr = $_GET['id'];
$text = isset($_POST['soft']) ? trim($_POST['soft']) : '';
if(!$text || strlen($text) < 4){
    include('../system/head.php');
    echo ''.$lang['vtext'].' | (min 5)'; break;
}

    mysql_query("INSERT INTO `mail` (
`id` ,
`author` ,
`adresat` ,
`text` ,
`chit` ,
`time` ,
`type`
)
VALUES (
NULL ,  '$uid',  '$pr',  '".mysql_real_escape_string($text)."', '0',  '".time()."', 'in'
);");


    mysql_query("INSERT INTO `mail` (
`id` ,
`author` ,
`adresat` ,
`text` ,
`chit` ,
`time` ,
`type`
)
VALUES (
NULL ,  '$uid',  '$pr',  '".mysql_real_escape_string($text)."', '0',  '".time()."', 'out'
);");

header('Location: http://'.$set['home'].'/mail/index.php?id='.$pr.'');

break;



case 'delete':
$pr = (int)$_GET['id'];
$zapros1 = mysql_query("SELECT * FROM `mail` WHERE `id` = '$pr'");
$est = mysql_fetch_assoc($zapros1);
if($rights == 1 && $uid == $est['author']){
    $zapros = mysql_query("SELECT * FROM `mail` WHERE `id` = '$pr'");
    if(!mysql_num_rows($zapros)){
        echo 'ERROR! This list doesn`t exists!';
    }else{
        $daa = mysql_fetch_assoc($zapros);
        mysql_query("DELETE FROM `mail` WHERE `id` = '$pr'");
        header('Location: http://'.$set['home'].'/mail/index.php?id='.$daa['adresat'].'');
    }
}else{
    echo 'ERROR! Permission danied!';
}

break;

}

if($nc['nick']) {$title = "$nc[nick]"; //Титл для низа страницы, для СЕО оптимизации желательно укзаывать
}
include('../system/foot.php');

/**
 * @author FROSTY (valik619)
 * @site nadmad.ru
 * @mail valik619@inbox.ru
 */

?>